﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;

namespace Demo_HHT_EARTH
{
    public partial class EMcheckInGroup : Form
    {
        SQLiteConnection cn2 = new SQLiteConnection();
        DataSet Lds = new DataSet();
        String strempgroup,strempcode = string.Empty;
        public EMcheckInGroup(string cklab)
        {
            InitializeComponent();
            if (cklab == "success")
            {
                label1.Visible = true;
                label1.Text = "Check In Equipment Successful";
                createDatagrid();
                textBox1.Focus();
            }
            else
            {
                label1.Visible = false;
                createDatagrid();
                textBox1.Focus();
            }
         
        }
       
        private void createDatagrid()
        {
            try
            {
                cn2.ConnectionString = "Data Source=|DataDirectory|db.sqlite";
            }
            catch (Exception)
            {

                MessageBox.Show("notconect SQLite");
            }
            string strcomm = "SELECT * FROM v_earth_employee_group";
            SQLiteCommand COMM = new SQLiteCommand(strcomm, cn2);
            SQLiteDataAdapter Lda = new SQLiteDataAdapter(COMM);
            Lds = new DataSet();
            cn2.Open();
            Lda.Fill(Lds, "L_emp");
            cn2.Close();
            dataGrid1.DataSource = null;
            dataGrid1.DataSource = Lds.Tables[0];
           
            
        }

        private void menuItem1_Click(object sender, EventArgs e)
        {
            
           
            DEMO_TEMP1.MainMenu Mn = new DEMO_TEMP1.MainMenu(true, 2);
            Mn.Show();
            this.Close();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
          
         
            if (strempgroup == "")
            {
                strempgroup= Lds.Tables[0].Rows[0]["employee_group"].ToString();
                EMcheckin EM = new EMcheckin(strempgroup);
                EM.Show();
                //this.Close();
            }
            else
            {
                EMcheckin EM = new EMcheckin(strempgroup);
                EM.Show();
               // this.Close();
            }
            
        }

        private void dataGrid1_Click(object sender, EventArgs e)
        {
           dataGrid1.Select(dataGrid1.CurrentRowIndex);
           strempgroup= Lds.Tables[0].Rows[dataGrid1.CurrentRowIndex]["employee_group"].ToString();
          
        }


        private void WorkScan(string strempcode)
        {
            bool ck_codeem = ValidateCode(ref strempcode);
            if (ck_codeem)
            {
                int check_in = ValidatedEmp_Code(strempcode);
                // Demo_HHT_EARTH.DataGetEmpCheckIn.emp_code = strempcode;
                if (check_in == 1)
                {
                    UpdateEmp_code(strempcode);
                    label1.Visible = true;
                    label1.Text = "Check In Employee Successful";
                    textBox1.Focus();
                }
                else if (check_in == -1)
                {
                    Demo_HHT_EARTH.CheckInConfirmTemp Emp_ConfTemp = new CheckInConfirmTemp(0);
                    Emp_ConfTemp.Show();

                }
                else if (check_in == 0)
                {
                    label1.Visible = true;
                    label1.Text = "Check In Employee Duplicate";
                    textBox1.Focus();
                }
                textBox1.Text = String.Empty;

            }
            else
            {
                label1.Visible = true;
                label1.Text = "Employee Code Error";
                textBox1.Focus();
            }
            
           
        }
       
        public bool ValidateCode(ref string strempcode)
        {
            int i = 1;
           
            string emc = string.Empty;

            try
            {
                for (int k = 0; k < strempcode.Length; k++)
                {
                    if (Char.IsLetterOrDigit(strempcode[k]))
                    {
                        emc += strempcode[k].ToString(); 
                    }
                }
                strempcode = emc;
               
            }
            catch (Exception)
            {

                
            }
            
            
            
            int emcl = emc.Length;
            if ((5 <= emcl)&& (emcl <= 7 ))
            {
                if (!Char.IsLetter(emc[0]))
                {
                    return false;
                }
                else
                {
                    
                    for ( i = 1; i < emcl; i++)
                    {
                        if (Char.IsDigit(emc[i]))
                        {
                            if (i == emcl)
                            {
                                return true;
                            }
                        }
                        else
                        {
                            return false;
                        }
                    }
                    if (i == emcl)
                    {
                        return true;
                    }
                    else
                    {
                        return false;  
                    }

                }
            }
            else
            {
                return false;
            }
            
        }

        public int ValidatedEmp_Code(string strempcode)
        {
            try
            {
                cn2.ConnectionString = "Data Source=|DataDirectory|db.sqlite";
            }
            catch (Exception)
            {

                MessageBox.Show("notconect SQLite");
            }
            string strcomm = "SELECT * FROM t_earth_flight_employee where employee_code = @strempcode";
            SQLiteCommand COMM = new SQLiteCommand(strcomm, cn2);
            COMM.Parameters.AddWithValue("@strempcode", strempcode);
            SQLiteDataAdapter Lda = new SQLiteDataAdapter(COMM);
            testT = new DataSet();
            cn2.Open();
            Lda.Fill(testT, "B");
            cn2.Close();
            if (testT.Tables[0].Rows.Count > 0 )
            {
                if (testT.Tables[0].Rows[0]["check_in_flag"].ToString()=="NO")
                {
                    Demo_HHT_EARTH.DataGetEmpCheckIn.emp_code = testT.Tables[0].Rows[0]["employee_code"].ToString();
                    dataGrid1.DataSource = null;
                    dataGrid1.DataSource = testT.Tables[0];
                    return 1; //update
                }
                else
                {
                    return 0; //duplicate
                }
                

            }
            else
            {
                Demo_HHT_EARTH.DataGetEmpCheckIn.emp_code = strempcode;
                return -1; //goshow
            }
            
        }
        DataSet testT = new DataSet();
        private void UpdateEmp_code(string strempcode)
        {
          
            try
            {
                cn2.ConnectionString = "Data Source=|DataDirectory|db.sqlite";
            }
            catch (Exception)
            {

                label1.Visible = true;
                label1.Text = "notconect SQLite";
               // MessageBox.Show("notconect SQLite");
            }
            
            string strcomm = "Update t_earth_flight_employee Set check_in_flag = 'YES',status ='ASSIGN' where employee_code = @strempcode";
            SQLiteCommand COMM = new SQLiteCommand(strcomm, cn2);
            COMM.Parameters.AddWithValue("@strempcode", strempcode);
            SQLiteDataAdapter Lda = new SQLiteDataAdapter(COMM);
            cn2.Open();
            COMM.ExecuteNonQuery();
            cn2.Close();
            createDatagrid();
           
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {


            if (e.KeyChar.ToString() != "\r")
            {
                strempcode += e.KeyChar.ToString();
            }
            else if (e.KeyChar.ToString() == "\r")
            {
                WorkScan(strempcode);
                strempcode = "";
            }
              
              
        }

        
        
    }
}